﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Reflection;

namespace BOSS.Service.Security
{
    /// <summary>
    /// 预定义权限列表
    /// </summary>
    public static class PredefinedPrivileges
    {
        #region 获取所有预定义权限

        /// <summary>
        /// 权限集合
        /// </summary>
        private static IDictionary<string, string> allPrivileges = null;

        /// <summary>
        /// 获取所有预定义权限
        /// </summary>
        public static IDictionary<string, string> AllPrivileges
        {
            get
            {
                if (allPrivileges == null)
                {
                    allPrivileges = new Dictionary<string, string>();
                    foreach (var fieldInfo in typeof(PredefinedPrivileges).GetFields(BindingFlags.Static | BindingFlags.Public))
                    {
                        if (fieldInfo.FieldType == typeof(string))
                        {
                            // 优先使用 DescriptionAttribute
                            object[] attributes = fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), true);
                            if (attributes.Length > 0)
                            {
                                DescriptionAttribute description = attributes[0] as DescriptionAttribute;
                                allPrivileges.Add(fieldInfo.GetValue(null).ToString(), description.Description);
                            }
                            else
                            {
                                allPrivileges.Add(fieldInfo.GetValue(null).ToString(), fieldInfo.Name);
                            }
                        }
                    }
                }
                return allPrivileges;
            }
        }

        /// <summary>
        /// 获取指定权限的描述信息
        /// </summary>
        /// <param name="privilegeName"></param>
        /// <returns></returns>
        public static string GetPrivilegeDescription(string privilegeName)
        {
            if (PredefinedPrivileges.AllPrivileges.ContainsKey(privilegeName))
            {
                return PredefinedPrivileges.AllPrivileges[privilegeName];
            }
            else
            {
                // return privilegeName;
                return "未定义权限 - " + privilegeName;
            }
        }

        #endregion

        #region

        /// <summary>
        /// 是否是群组主管
        /// <c>account.group.manager</c>
        /// </summary>
        public const string ACCOUNT_GROUP_MANAGER = "account.group.manager";

        #endregion

        /// <summary>
        /// BOSS 系统的整体权限
        /// <c>boss</c>
        /// </summary>
        [Description("可以登录并使用 BOSS 系统")]
        public const string BOSS = "boss";

        #region 客户管理

        /// <summary>
        /// 客户管理
        /// <c>boss.customer</c>
        /// </summary>
        public const string BOSS_CUSTOMER = "boss.customer";

        /// <summary>
        /// 客户管理-新增客户
        /// <c>boss.customer.create</c>
        /// </summary>
        public const string BOSS_CUSTOMER_CREATE = "boss.customer.create";

        #endregion

        #region 产品管理

        /// <summary>
        /// 产品管理
        /// <c>boss.product</c>
        /// </summary>
        public const string BOSS_PRODUCT = "boss.product";

        /// <summary>
        /// 产品管理 - 新增产品
        /// <c>boss.product.create</c>
        /// </summary>
        public const string BOSS_PRODUCT_CREATE = "boss.product.create";

        /// <summary>
        /// 产品管理 - 产品查询
        /// <c>boss.product.search</c>
        /// </summary>
        public const string BOSS_PRODUCT_SEARCH = "boss.product.search";

        /// <summary>
        /// 产品管理 - 购买产品
        /// <c>boss.product.purchase</c>
        /// </summary>
        public const string BOSS_PRODUCT_PURCHASE = "boss.product.purchase";

        /// <summary>
        /// 产品管理 - 消费产品
        /// <c>boss.product.consume</c>
        /// </summary>
        public const string BOSS_PRODUCT_CONSUME = "boss.product.consume";

        #endregion

        #region 系统管理

        /// <summary>
        /// 系统管理
        /// <c>boss.system</c>
        /// </summary>
        public const string BOSS_SYSTEM = "boss.system";
        
        #endregion
    }
}
